package utils

import (
	"context"
	"fmt"
	"log"

	"github.com/redis/go-redis/v9"

	beego "github.com/beego/beego/v2/server/web"

	"github.com/zeromicro/go-zero/core/discov"
	"github.com/zeromicro/go-zero/zrpc"
)

// get redis client
func GetRedisClient() *redis.Client {
	redishost, _ := beego.AppConfig.String("redishost")
	redisport, _ := beego.AppConfig.String("redisport")
	redisClient := redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%s", redishost, redisport),
		DB:   0,
	})
	// 测试reids连接是否成功
	log.Print(redisClient.Ping(context.Background()).Err())

	return redisClient
}

// get rpc client
func GetRpcClient(rpc_key string) zrpc.Client {
	etcdhost, _ := beego.AppConfig.String("etcdhost")
	etcdport, _ := beego.AppConfig.String("etcdport")
	return zrpc.MustNewClient(zrpc.RpcClientConf{
		Etcd: discov.EtcdConf{ // 通过 etcd 服务发现时，只需要给 Etcd 配置即可
			Hosts: []string{
				fmt.Sprintf("%s:%s", etcdhost, etcdport),
			},
			Key: rpc_key,
		},
	})
}
